import re
import os
import time
import oss2
from oss2.models import BucketReferer
# 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth('LTAI4FreXSrQheqtqsKeR2iR', 'hjGZJVv6vnN2BwkQBGzE6n4oq8hqrZ')
# Endpoint以杭州为例，其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'oss-cn-qingdao-internal.aliyuncs.com', 'fzj-202001')
bucket.put_bucket_referer(BucketReferer(True, ['aliyun.com', '*.aliyuncs.com','mdzimu.com','*.mdzimu.com','*.console.aliyun.com','mdzimu.com/*']))
dir='/vdb/python/csdn'
dir_file_list=os.listdir(dir)
file_list=[]
re_file_com=re.compile('uwsgi\d?.log')
for file in dir_file_list:
    path = os.path.join(dir,file)
    if os.path.isfile(path) and re_file_com.match(file):
        file_list.append(path)
uwsgi_line_re_com=re.compile('{address space usage: (\d+) bytes/\d+MB} {rss usage: (\d+) bytes/\d+MB} \[pid: (\d+)\|app: \d+\|req: \d+/\d+\] \d+.\d+.\d+.\d+ \(\) {(\d+) vars in (\d+) bytes} \[(\w+ \w+ \d+ \d+:\d+:\d+ \d+)\] GET (.*?) => generated (\d+) bytes in (\d+) msecs \(HTTP/\d\.\d (\d+)\) (\d+) headers in (\d+) bytes \(\d+ switches on core \d+\)')
for filename in file_list:
    with open(filename,'rb') as f:
        line=f.readlines()
        bucket.put_object(filename.replace(dir,'csdn/'+time.strftime('%Y-%m-%d',time.localtime())), b''.join(line))
    open(filename,'w')
